<?php

class mitem extends CI_Model {
    
	public function mitem(){
	$this->load->library('serversidedatatables');
	$this->setVars();
	}
	
	        function setVars()
    {
        $this->result = '';
        $this->idnya = 0;
    }
	
    public function create() {
        $data = array(
            'name'  => $this->input->post( 'cName', true ),
            'email' => $this->input->post( 'cEmail', true )
        );
        
        $this->db->insert( 't_mtr_item_mapping', $data );
        return $this->db->insert_id();
    }
    
    public function getById( $id ) {
    $id = intval( $id );
	$item_id = $this->db->where('item_code',"$id")->limit( 1 )->get( 't_mtr_item_mapping' );
        $query = $item_id;
	
        if( $query->num_rows() > 0 ) {
            return $query->row();
        } else {
            return array();
        }
    }
    
    public function getAll() {
	
        //get all records from users table
	$sql="select 
	imap.item_code,
	imap.item_name, 
	imap.item_category_id , 
	grp.item_group_id,
	grp.item_group_name, 
	iss.category_name as isisname,
	iss.category_id as isisid, 
	imap.default_price as price , regexp_replace(regexp_replace(regexp_replace(regexp_replace(imap.default_price::numeric::money::text, '[$]', 'Rp. ', 'g')::text,',','|','g'),'[.]',',','g'),'[|]','.','g') as default_price  
	from 
	t_mtr_item_mapping imap  
	LEFT JOIN 
	t_mtr_item_group grp on grp.item_group_id = imap.item_group_id 
	left join
	t_mtr_item_isis_category iss on iss.category_id = imap.category_id";

	// $query = $this->db->get();
    $query = $this->db->query($sql);
	
        if( $query->num_rows() > 0 ) {
            return $query->result();
        } else {
            return array();
        }
    } //end getAll
    
   public function getItem(){
	$this->db->from('t_mtr_item_mapping');
	$this->db->from('t_mtr_item_group');
	$query = $this->db->get();
        if( $query->num_rows() > 0 ) {
            return $query->result();
        } else {
            return array();
        }

   }
   
   function coba_ssp(){
	$sIndexColumn = "item_code"; //index column untuk kecepatan akses data
    $defaultOrder = "0"; //default order berdasarkan field yg ada pada $aColumns
    $userlogin = $this->session->userdata('username'); //user credentials
    $cluster_id = $this->mglobal->user_cluster($userlogin);
    $flag = '';
	//ambil field2 dari tabel t_rpt_activity_real_time yg akan ditampilkan pada dataTables
    //$aColumns = array("imap.item_code","imap.item_name","grp.item_group_name","imap.default_price","iss.category_name"); 
    $aColumns = array("item_code","item_name","default_price","item_group_name","isis_name");
	//get all records from users table
	/*$inputQuery="select 
	imap.item_code,
	imap.item_name, 
	imap.item_category_id , 
	grp.item_group_id,
	grp.item_group_name, 
	iss.category_name as isisname,
	iss.category_id as isisid, 
	imap.default_price as price , regexp_replace(regexp_replace(regexp_replace(regexp_replace(imap.default_price::numeric::money::text, '[$]', 'Rp. ', 'g')::text,',','|','g'),'[.]',',','g'),'[|]','.','g') as default_price  
	from 
	t_mtr_item_mapping imap  
	LEFT JOIN 
	t_mtr_item_group grp on grp.item_group_id = imap.item_group_id 
	left join
	t_mtr_item_isis_category iss on iss.category_id = imap.category_id";
	*/
	// $inputQuery="select * from f_ssp_matcode2";
	$inputQuery="SELECT " . implode(',' , array_values($aColumns))." FROM v_ssp_matcode";
	// $query = $this->db->get();
    /*$query = $this->db->query($sql);
	
        if( $query->num_rows() > 0 ) {
            return $query->result();
        } else {
            return array();
        }*/
	return $this->serversidedatatables->executeServerSideProcess($defaultOrder, $flag, $userlogin, $inputQuery, $aColumns, $sIndexColumn, 'asc');	
    } //end getAll
   
   public function getValueFromTable(){
	$this->db->distinct();
	$this->db->select('t_mtr_item_group.item_group_id, t_mtr_item_group.item_group_name');
	$this->db->from('t_mtr_item_group');
	//$this->db->join('t_mtr_item_group', 't_mtr_item.item_group_id = t_mtr_item_group.item_group_id');
	$this->db->order_by('item_group_id',asc);
	$query = $this->db->get();
	
	return $query->result();    
   }
   
   public function getValueFromTableIsis(){
	$this->db->select('*');
	$this->db->from('t_mtr_item_isis_category');
	//$this->db->join('t_mtr_item_group', 't_mtr_item.item_group_id = t_mtr_item_group.item_group_id');
	//$this->db->order_by('item_group_id',asc);
	$query = $this->db->get();
	
	return $query->result();    
   }
   

    public function getValueFromTableCategory(){
    // $this->db->distinct();
    $this->db->select('dbr_id, dbr_name');
    $this->db->from('t_mtr_item_category');
    //$this->db->join('t_mtr_item_group', 't_mtr_item.item_group_id = t_mtr_item_group.item_group_id');
    $this->db->order_by('dbr_id',asc);
    $query = $this->db->get();
    
    return $query->result();    
   }

   public function getValueFromID($id){
	$this->db->from('t_mtr_item_mapping');
	$this->db->from('t_mtr_item_group');
	$this->db->where('item_mapping_id',$id);
	$query = $this->db->get();
	return $query->row_array();    
   }
   
   
    public function update($id,$default_price,$dbr_id,$item_category_id)
	{
	//$item_group_id,$category_id,$item_category_id,$default_price
    $cur_date = date("Y-m-d");
	$update_by = $this->session->userdata('username');
	$dbr_id = $this->input->post('dbr_id');
	$item_category_id = $this->input->post('item_category_id');
	$default_price = $this->input->post('default_price');
        
	$data = array(
        'item_group_id' => $id,
        'item_category_id' => $item_category_id,
	    'updated_on' => $cur_date,
	    'updated_by' => $update_by,
	    'default_price'=>$default_price,
		'dbr_id'=>$dbr_id
        );
	
	//$this->db->where('item_code',$icode);
        $this->db->update( 't_mtr_item_mapping', $data, array( 'item_code' => $this->input->post( 'id', true ) ) );
	
    }
    
    
   public function getItemGroupName($id){
	$this->db->select('item_group_name');
	$this->db->from('t_mtr_item_group');
	$this->db->where('item_group_id',$id);
	$query = $this->db->get();
	return $query->row_array();    
   
   }

   public function getItemCode($id)
   {
	$this->db->select('item_code');
	$this->db->from('t_mtr_item_mapping');
	$this->db->where('item_mapping_id',$id);
	$query = $this->db->get();
	return $query->row();
   }
    
    public function delete( $id ) {
        /*
        * Any non-digit character will be excluded after passing $id
        * from intval function. This is done for security reason.
        */
        $id = intval( $id );
        
        $this->db->delete( 't_mtr_item', array( 'item_id' => $id ) );
    } //end delete
    
} //end class